﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using Dictionary;

namespace Dictionary.DataLayer
{
    public class LoadData
    {
        //Ham lay ten cua cac bo tu dien
        public static string NameDict(string fn)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath +"\\" +fn);
                XmlNodeList list = source.GetElementsByTagName("Name");
                if (list.Count > 0)
                {
                    return list[0].InnerText;
                }
                return null;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return null;
            }
        }

        //Ham lay danh sach cac tu vung
        public static ArrayList ListWord(string fn)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath + "\\" + fn);
                XmlNodeList list = source.GetElementsByTagName("English");
                ArrayList listWord = new ArrayList();
                if (list.Count >0)
                {
                    string[] word = new string[list.Count];
                    int i = 0;
                    foreach (XmlNode w in list)
                    {
                        word[i] = w.InnerText;
                        i++;
                    }
                    Array.Sort(word);
                    foreach (string w in word)
                    {
                        listWord.Add(w);
                    }
                }
                return listWord;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return null;
            }
        }

        //Ham lay noi dung cua tu vung
        public static ArrayList Content(string fn, string word)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath + "\\" + fn);
                XmlNode node = source.SelectSingleNode("/Dictionary/Word[English=\"" + word + "\"]");
                ArrayList cnt = new ArrayList();
                if (node == null)
                {
                    return null;
                }
                else
                {
                    foreach (XmlElement element in node)
                        cnt.Add(element.InnerText);

                    return cnt;
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return null;
            }
        }

        //Ham kiem tra xem file xml co dung cau truc hay k
        public static bool CheckDicContent(string fn)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(fn);
                XmlNodeList node;
                node= source.GetElementsByTagName("Dictionary");
                if (node.Count <= 0)
                {
                    return false;
                }
                node = source.GetElementsByTagName("Name");
                if (node.Count <= 0)
                {                  
                    return false;
                }

                return true;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return false;
            }
        }
    }
}
